package appapi.controller.oider;

import appapi.common.ResponseBean;
import appapi.entity.mqtt.Areapath;
import appapi.entity.mqtt.Devices;
import appapi.entity.mqtt.mqttvo.AreaVo;
import appapi.service.AreapathService;
import appapi.service.DevicesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Ygp Work
 * @description:
 * @date: 2023-07-07 10:22
 */
@Api(tags = "地区接口")
@RestController
@RequestMapping("/api/areapath")
@CrossOrigin
public class AreapathController {
    @Autowired
    private AreapathService areapathService;

    @GetMapping
    @ApiOperation(value = "查询所有的地区")
    @CrossOrigin
    public ResponseBean<List<Areapath>> getAllDevices(Areapath areapath) {
        List<Areapath> devicesList = areapathService.getAll(areapath);
        int size = devicesList.size();
        if (!devicesList.isEmpty()) {
            return ResponseBean.success(size, devicesList);
        } else {
            return ResponseBean.fail("查询失败，设备列表为空");
        }
    }


    @PostMapping
    @ApiOperation(value = "更新地区")
    @CrossOrigin
    public ResponseBean insertDevices(@RequestBody Areapath areapath) {
        areapathService.insertAreapath(areapath);
        return ResponseBean.success();
    }

    @ApiOperation(value = "根据id查询地区下的设备")
    @GetMapping("/ids")
    @CrossOrigin
    public ResponseBean getAreapathById(Long id) {
        List<AreaVo> areapathByid = areapathService.getAreapathByid(id);
        int size = areapathByid.size();
        if (size > 0) {
            return ResponseBean.success(size, areapathByid);
        } else {
            return ResponseBean.fail("查询失败，找不到指定的设备");
        }
    }

}
